//首页的js逻辑
var article_title = document.querySelector('.article_title')
var article_classify = document.querySelector('.tagsList')
var article_content = document.querySelector('.content')
var article_time = document.querySelector('#pushTime')
var footer_list = document.querySelector('.newArticleList')
var footer_pages = document.querySelector('.newArticlePageList')
var hot_list = document.querySelector('.hotList')
var article_continue = document.querySelector('.content-continue')

//点击继续阅读全文按钮
article_continue.addEventListener('click' , function(){
    var id = this.getAttribute('data-id')
    location.href = '/getPage?id=' + id
})

//获取最新的第一篇文章
myAjax('GET', '/getArticle', null).then((e) => {
    var obj = JSON.parse(e)
    article_title.innerText = obj.title
    article_continue.setAttribute("data-id" , obj._id)
    article_classify.innerText = obj.classify
    article_content.innerText = obj.content.slice(0, 100) + '...'
    article_time.innerText = obj.time
})
//获取最新的文章分页内容
myAjax('GET', '/getNewArticleList?page=1', null).then((e) => {
    var obj = JSON.parse(e)
    for (var i = 0; i < obj.length; i++) {
        var item = obj[i]
        var li = document.createElement('li')
        li.setAttribute('data-id' , item._id)
        li.addEventListener('click' , function(){
            var id = this.getAttribute('data-id')
            location.href = '/getPage?id=' + id
        })
        li.innerText = item.title;
        li.className = 'footer_item'
        footer_list.appendChild(li)
    }
})

//获取文章的数量，实现分页
myAjax('GET', '/ArticleNums', null).then((e) => {
    for (var i = 1; i <= parseInt(e); i++) {
        var li = document.createElement('li')
        li.className = 'pages'
        li.innerText = i;
        li.addEventListener('click', function () {
            var num = this.innerText;
            myAjax('GET', '/getNewArticleList?page=' + num, null).then((e) => {
                var obj = JSON.parse(e)
                footer_list.innerHTML = ""
                for (var i = 0; i < obj.length; i++) {
                    var item = obj[i]
                    var li = document.createElement('li')
                    li.setAttribute('data-id' , item._id)
                    li.addEventListener('click' , function(){
                        var id = this.getAttribute('data-id')
                        location.href = '/getPage?id=' + id
                    })
                    li.innerText = item.title;
                    li.className = 'footer_item'
                    footer_list.appendChild(li)
                }
            })
        })
        footer_pages.appendChild(li)
    }
})

//渲染热度最高的文章(点击次数)
myAjax('GET' , '/getHotArticleList',null).then((e)=>{
    var arr = JSON.parse(e)
    for(var i = 0 ; i < arr.length ; i++){
        var item = arr[i]
        var li = document.createElement('li')
        li.setAttribute('data-id' , item._id)
        li.addEventListener('click' , function(){
            var id = this.getAttribute('data-id')
            location.href = '/getPage?id=' + id
        })
        li.innerText = item.title;
        li.className = 'hot_item'
        hot_list.appendChild(li)
    }
})

